/**
 * @param {number[][]} grid
 * @return {number}
 */
var maxAreaOfIsland = function (grid) {
  let m = grid.length
  let n = grid[0].length
  let result = 0
  let dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]]
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      if (grid[i][j] === 1) {
        let ans = 0
        let queue = [[i, j]]
        while (queue.length) {
          let [x, y] = queue.shift()
          ans++
          grid[x][y] = 0
          for (let [dx, dy] of dirs) {
            let nx = x + dx
            let ny = y + dy
            if (nx < 0 || nx >= m || ny < 0 || ny >= n || grid[nx][ny] === 0) {
              continue
            }
            queue.push([nx, ny])
            grid[nx][ny] = 0
          }
        }
        result = Math.max(ans, result)
      }
    }
  }
  return result
};
let grid = [[1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 1, 1], [0, 0, 0, 1, 1]]
console.log(maxAreaOfIsland(grid))